package com._21cn.cms.article.entity;

import com._21cn.cms.article.Constants;
import com._21cn.cms.article.entity.vo.ListArticle;
import com._21cn.cms.article.utils.FormatContentUtil;
import com._21cn.cms.site.entity.Channel;
import com._21cn.cms.site.entity.Column;
import com._21cn.cms.site.entity.Site;
import com._21cn.framework.utils.DateUtil;
import com._21cn.framework.utils.StringUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Article extends ListArticle implements Serializable {
	private static final long serialVersionUID = -1L;
    private Pattern ARTICLE_URL_PATTERN = Pattern.compile("/(\\d{4})/(\\d{4})/(\\d{2})/(\\d{1,10})\\.shtml");

	private Integer id = 0;
    private Integer articleId = 0;
	private Integer siteId = 1;
    private Integer channelId = 0;
    private Integer columnId = 0;
	private String title = "";
	private String titleShort = "";
	private String summary = "";
	private String keyword = "";
	private String sourceName = "";
	private String author = "";
	private String firstPicUrl = "";
    private String articleUrl = "";
	private Integer articleType = Constants.ARTICLE_TYPE_COMMON;        //1资讯
	private Date publishTime = new Date();
	private Integer publishStatus = 0;
	private Integer createUserId = 0;
	private Date createTime = new Date();
	private Integer modifyUserId = 0;
	private Date modifyTime = new Date();
	private Integer clickNum = 0;
	private Integer summaryFlag = 0;
	private Integer reviewFlag = 0;
	private Integer picFlag = 0;
    private String content = "";
    private List<ArticlePicture> articlePictureList = new ArrayList<ArticlePicture>();      //图片列表
    private List<ArticleThumbPicture> articleThumbPictureList = new ArrayList<ArticleThumbPicture>();       //缩略图列表
    private String thumbPicUrlListStr;      //缩略图
    //额外属性
    private Column column = new Column();
    private Channel channel = new Channel();
    private Site site = new Site();
    private String mainColumn = "";
    private String mainChannel = "";
    private Integer mainColumnId = 0;
    private String relateColumns = "";
    private String relateColumnIds = "";
    private List<Integer> relateColumnIdList = new ArrayList<Integer>();
    private String regions = "";
    private String regionIds = "";
    private Integer topTime = DateUtil.getCurrent10TimeStamp();
    private String articleWapUrl = "";
    private String articleAppUrl = "";
    private List<Integer> regionIdList = new ArrayList<Integer>();
    private List<String> pageContentList = new ArrayList<String>();
    private Long publishTimestamp = 0L;
    private String publishTimeStr = "";
    private List<String> keywordList = new ArrayList();
    private String topTimeStr = "";

    public String getTopTimeStr() {
        if( topTime>0 ){
            topTimeStr = DateUtil.getDateString(topTime);
        }
        return topTimeStr;
    }

    public void setTopTimeStr(String topTimeStr) {
        this.topTimeStr = topTimeStr;
    }

    public String getPublishTimeStr() {
        return getPublishTimeStr("yyyy-MM-dd HH:mm");
    }

    public String getPublishTimeStr(String pattern) {
        if (this.publishTime != null) {
            this.publishTimeStr = DateUtil.formatDate(this.publishTime, pattern);
        }

        return this.publishTimeStr;
    }

    public void setPublishTimeStr(String publishTimeStr) {
        this.publishTimeStr = publishTimeStr;
    }

    public List<String> getKeywordList() {
        if (!StringUtil.isEmpty(this.keyword)) {
            this.keywordList = StringUtil.str2List(this.keyword);
        }
        return this.keywordList;
    }

    public void setKeywordList(List<String> keywordList) {
        this.keywordList = keywordList;
    }

    public Long getPublishTimestamp() {
        if (this.publishTime != null) {
            this.publishTimestamp = Long.valueOf(this.publishTime.getTime());
        }
        return this.publishTimestamp;
    }

    public void setPublishTimestamp(Long publishTimestamp) {
        this.publishTimestamp = publishTimestamp;
    }

    public String getArticleWapUrl() {
        if (!StringUtil.isEmpty(this.articleUrl)) {
            Matcher matcher = this.ARTICLE_URL_PATTERN.matcher(this.articleUrl);
            if (matcher.find())
                this.articleWapUrl = this.articleUrl.replace("/a/", "/m/");
            else {
                this.articleWapUrl = this.articleUrl;
            }
        }
        return this.articleWapUrl;
    }

    public void setArticleWapUrl(String articleWapUrl) {
        this.articleWapUrl = articleWapUrl;
    }

    @Override
    public String getArticleAppUrl() {
        if (!StringUtil.isEmpty(this.articleUrl)) {
            Matcher matcher = this.ARTICLE_URL_PATTERN.matcher(this.articleUrl);
            if (matcher.find())
                this.articleAppUrl = this.articleUrl.replace("/a/", "/c/");
            else {
                this.articleAppUrl = this.articleUrl;
            }
        }
        return this.articleAppUrl;
    }

    @Override
    public void setArticleAppUrl(String articleAppUrl) {
        this.articleAppUrl = articleAppUrl;
    }

    @Override
    public Integer getArticleId() {
        if(id!=articleId){
            return id;
        }
        return articleId;
    }

    @Override
    public void setArticleId(Integer articleId) {
        this.articleId = articleId;
        this.id = articleId;
    }

    public String getMainChannel() {
        return mainChannel;
    }

    public void setMainChannel(String mainChannel) {
        this.mainChannel = mainChannel;
    }

    public Integer getTopTime() {
        return topTime;
    }

    public void setTopTime(Integer topTime) {
        this.topTime = topTime;
    }

    public Site getSite() {
        return site;
    }

    public void setSite(Site site) {
        this.site = site;
    }

    public List<String> getPageContentList() {
        return pageContentList;
    }

    public void setPageContentList(List<String> pageContentList) {
        this.pageContentList = pageContentList;
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public Integer getChannelId() {
        if( channel!=null && channel.getId()>0 ){
            return channel.getId();
        }
        return channelId;
    }

    public void setChannelId(Integer channelId) {
        this.channelId = channelId;
    }

    public Integer getColumnId() {
        return columnId;
    }

    public void setColumnId(Integer columnId) {
        this.columnId = columnId;
    }

    public Column getColumn() {
        return column;
    }

    public void setColumn(Column column) {
        this.column = column;
    }

    public String getThumbPicUrlListStr() {
        return thumbPicUrlListStr;
    }

    public void setThumbPicUrlListStr(String thumbPicUrlListStr) {
        this.thumbPicUrlListStr = thumbPicUrlListStr;
    }

    public List<ArticlePicture> getArticlePictureList() {
        return articlePictureList;
    }

    public void setArticlePictureList(List<ArticlePicture> articlePictureList) {
        this.articlePictureList = articlePictureList;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<ArticleThumbPicture> getArticleThumbPictureList() {
        return articleThumbPictureList;
    }

    public void setArticleThumbPictureList(List<ArticleThumbPicture> articleThumbPictureList) {
        this.articleThumbPictureList = articleThumbPictureList;
    }

    public void setId(Integer id) {
        this.id = id;
        this.articleId = id;
    }

    public Integer getId() {
        return this.id;
    }

    public void setSiteId(Integer siteId) {
        this.siteId = siteId;
    }

    public Integer getSiteId() {
        if( site!=null && site.getId()>0){
            return site.getId();
        }
        return this.siteId;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitleShort(String titleShort) {
        this.titleShort = titleShort;
    }

    public String getTitleShort() {
        return this.titleShort;
    }

    public String getTitleShort(int length){
        if(!StringUtil.isEmpty(titleShort) && titleShort.length()<length){
            return titleShort;
        }
        return FormatContentUtil.subString(title, length, "...");
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getSummary() {
        return this.summary;
    }

    public String getSummary(int size) {
        return StringUtil.cutString(this.summary, size);
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    public String getKeyword() {
        return this.keyword;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getSourceName() {
        return this.sourceName;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setFirstPicUrl(String firstPicUrl) {
        this.firstPicUrl = firstPicUrl;
    }

    public String getFirstPicUrl() {
        return this.firstPicUrl;
    }

    public String getFirstPicUrl(String code) {
        if (this.articleThumbPictureList.size() > 0) {
            this.firstPicUrl = ((ArticleThumbPicture)this.articleThumbPictureList.get(0)).getPicUrl();
        }
        this.firstPicUrl = super.getFirstPicUrl(code);
        return this.firstPicUrl;
    }

    public void setArticleUrl(String articleUrl) {
        this.articleUrl = articleUrl;
    }

    public String getArticleUrl() {
        return this.articleUrl;
    }

    public String getArticleUrl( int pageNo ){
        return makeArticleUrl( column, pageNo );
    }

    public String getArticleUrl( Column column ){
        return makeArticleUrl( column, 0 );
    }

    public void setArticleType(Integer articleType) {
        this.articleType = articleType;
    }

    public Integer getArticleType() {
        return this.articleType;
    }

    public void setPublishTime(java.util.Date publishTime) {
        this.publishTime = publishTime;
    }

    public java.util.Date getPublishTime() {
        return this.publishTime;
    }

    public void setPublishStatus(Integer publishStatus) {
        this.publishStatus = publishStatus;
    }

    public Integer getPublishStatus() {
        return this.publishStatus;
    }

    public void setCreateUserId(Integer createUserId) {
        this.createUserId = createUserId;
    }

    public Integer getCreateUserId() {
        return this.createUserId;
    }

    public void setCreateTime(java.util.Date createTime) {
        this.createTime = createTime;
    }

    public java.util.Date getCreateTime() {
        return this.createTime;
    }

    public void setModifyUserId(Integer modifyUserId) {
        this.modifyUserId = modifyUserId;
    }

    public Integer getModifyUserId() {
        return this.modifyUserId;
    }

    public void setModifyTime(java.util.Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public java.util.Date getModifyTime() {
        return this.modifyTime;
    }

    public void setClickNum(Integer clickNum) {
        this.clickNum = clickNum;
    }

    public Integer getClickNum() {
        return this.clickNum;
    }

    public void setSummaryFlag(Integer summaryFlag) {
        this.summaryFlag = summaryFlag;
    }

    public Integer getSummaryFlag() {
        return this.summaryFlag;
    }

    public void setReviewFlag(Integer reviewFlag) {
        this.reviewFlag = reviewFlag;
    }

    public Integer getReviewFlag() {
        return this.reviewFlag;
    }

    public void setPicFlag(Integer picFlag) {
        this.picFlag = picFlag;
    }

    public Integer getPicFlag() {
        return this.picFlag;
    }

    public String getMainColumn() {
        return mainColumn;
    }

    public void setMainColumn(String mainColumn) {
        this.mainColumn = mainColumn;
    }

    public Integer getMainColumnId() {
        return mainColumnId;
    }

    public void setMainColumnId(Integer mainColumnId) {
        this.mainColumnId = mainColumnId;
    }

    public String getRelateColumns() {
        return relateColumns;
    }

    public void setRelateColumns(String relateColumns) {
        this.relateColumns = relateColumns;
    }

    public String getRelateColumnIds() {
        return relateColumnIds;
    }

    public void setRelateColumnIds(String relateColumnIds) {
        this.relateColumnIds = relateColumnIds;
    }

    public List<Integer> getRelateColumnIdList() {
        return relateColumnIdList;
    }

    public void setRelateColumnIdList(List<Integer> relateColumnIdList) {
        this.relateColumnIdList = relateColumnIdList;
    }

    public String getRegions() {
        return regions;
    }

    public void setRegions(String regions) {
        this.regions = regions;
    }

    public String getRegionIds() {
        return regionIds;
    }

    public void setRegionIds(String regionIds) {
        this.regionIds = regionIds;
    }

    public List<Integer> getRegionIdList() {
        return regionIdList;
    }

    public void setRegionIdList(List<Integer> regionIdList) {
        this.regionIdList = regionIdList;
    }

    public String regulateArticleUrl(){
        try {
            if(!StringUtil.isEmpty(articleUrl) &&
                    !articleUrl.contains(com._21cn.cms.common.Constants.DEFAULT_SITE_HOST)){
                return articleUrl;
            }
            articleUrl = makeArticleUrl( column, 0 );
        }
        catch( Exception e ) {
            e.printStackTrace();
        }
        return articleUrl;
    }

    private String makeArticleUrl( Column column, int pageNo ){
        if( column ==null || StringUtil.isEmpty(column.getUrl()))
            return "0.shtml";

        StringBuilder sb = new StringBuilder( column.getUrl() );
        String clientCode = Constants.CLIENT_CODE_PC;
        sb.append("/").append(clientCode).append("/");
        sb.append(DateUtil.formatDate( getPublishTime(), "yyyy/MMdd/HH/"));
        sb.append( getId());

        if( pageNo>0 && pageNo< pageContentList.size() ){
            sb.append( "_" ).append( pageNo );
        }
        sb.append(".shtml");
        return sb.toString();
    }

}